var regexp = "^[a-zA-Z0-9\ \.\*\+\-\|]+$";
export const card = {
        p:1,
		 
		//分享图高度
		getShareHeight(){
			let shareHeight;
			let res = uni.getSystemInfoSync();
			let w = res.windowWidth;
			let h = res.windowHeight;
			
			
			// 减去 底部实际px  底部留空  顶部留空
			shareHeight = h -    210 * w / 750  - 40  - 118 * w / 750 ;
			if(shareHeight){
				return shareHeight;
			}
			return false
			
		},
		designWordHeight(word,lineHeight, TextContentWidth,fontSize,designWidth){
			
			 
			let p = 1;
			let lineWidth = 0;
			let outHeight= lineHeight;
			
			for (let i = 0; i < word.length; i++) {
				
				
				var re = new   RegExp(regexp) ;
				if (re.test(word.substring( i,  i+1))){
					console.log(word.substring( i,  i+1),"true")
					lineWidth += parseInt(fontSize*p*2/3);
				}else{
					console.log(word.substring( i,  i+1),"false")
					console.log(word.substring( i,  i+1),"false")
					
					lineWidth += parseInt(fontSize*p);
				}
				 
			    
			    if (lineWidth > parseInt(TextContentWidth*p)) {
			        outHeight += parseInt(lineHeight*p);
			        lineWidth = 0;
			    }
			}
			
			return outHeight;
		},
		
        canvasTextAutoLine(word, ctx, initX, initY, lineHeight, maxWidth,fontSize,color) {
            console.log(word)
            ctx.setTextAlign('left');
            let  currW=parseInt(initX*this.p);
            initY=parseInt(initY*this.p);
            ctx.setFontSize(parseInt(fontSize*this.p));
            if(!color) color="#333";
            ctx.setFillStyle(color);
            let lineWidth = 0;
			var re = new   RegExp(regexp) ;
            for (let i = 0; i < word.length; i++) {
                // console.log(word.substring( i,  i+1),currW,initY);
                
				
				console.log(word.substring( i,  i+1),currW,initY);
				
				
				if (re.test(word.substring( i,  i+1))){
					lineWidth += parseInt(fontSize*this.p*2/3);
				}else{
					lineWidth += parseInt(fontSize*this.p);
				}
                if (lineWidth > parseInt(maxWidth*this.p)) {
                    initY += parseInt(lineHeight*this.p);
                    lineWidth = 0;
                    currW= parseInt(initX*this.p);
					ctx.fillText(word.substring( i,  i+1), currW, initY);
				
					if (re.test(word.substring( i,  i+1))){
						currW += parseInt(fontSize*this.p*2/3);
						lineWidth += parseInt(fontSize*this.p*2/3);
					}else{
						currW += parseInt(fontSize*this.p);
						lineWidth += parseInt(fontSize*this.p);
					}
					

                }else{

                    ctx.fillText(word.substring( i,  i+1), currW, initY);
					
					if (re.test(word.substring( i,  i+1))){
						currW += parseInt(fontSize*this.p*2/3);
					}else{
						currW += parseInt(fontSize*this.p);
					}
					 

                }


            }
            ctx.save()

        },

        writeWord(word,l,t,size,color,ctx,align,alpha){
			if(!word){return ;}
            console.log(word);
            if(!align){align="left"}
            if(!alpha){alpha=1}
            ctx.setGlobalAlpha(alpha)
            ctx.setTextAlign(align);
            ctx.setFontSize(size*this.p);
            ctx.setFillStyle(color);
            if(typeof  ctx.setTextBaseline==="function")ctx.setTextBaseline('middle');
            ctx.fillText( word, l* this.p, t* this.p);

        },
        configCard(_w,_h,_this,ctx,fun,loadingTxt){
			
            if(!loadingTxt){
                loadingTxt= "图片加载中";
            }
			uni.showLoading({
				title: loadingTxt
			})
            
            setTimeout(function () {
                uni.hideLoading()
            }, 4000);

            let res = uni.getSystemInfoSync();
            let w = res.windowWidth;
            let h  =  _h/  _w* w;
            this.p = w / _w;
			_this.w= w;
			_this.h=h;
            
			
				 ctx.setFillStyle('#ffffff');
				 ctx.fillRect(0, 0, w, h);
				 ctx.save();
				 fun();
			
           
        },
		drawRectangle(ctx,l,t, w, h,bgColor){
			let p=this.p;
			
			ctx.setFillStyle(bgColor);
			ctx.fillRect(l* p,t* p, w* p, h* p);
			ctx.save();
		},
        saveCardImg(url, l, t, w, h, ctx, success) {
			console.log(url,'图片路径')
			if(!url){
				console.log(url)
				uni.showToast({
					title:"图片信息有误！"
				})
				;
				return
			}
            let p=this.p;
			
			url= url.replace('http://','https://')
			
		 
            uni.downloadFile({
                url: url,
                success: function (avatarres) {
                    ctx.drawImage(avatarres.tempFilePath, l * p, t * p, w * p, h * p);
                    ctx.save();
                    success()
                }
            })

        },
		
		saveCardImgClip(url, l, t, w, ctx, success) {
		    let p=this.p;
			
			url= url.replace('http://','https://')
			
			  
		 
		    uni.downloadFile({
		        url: url,
		        success: function (avatarres) {
					 let __w = parseInt(w * p);
					ctx.beginPath();
					ctx.arc(l * p + __w / 2, t * p + __w / 2, __w / 2, 0, 2 * Math.PI);
					ctx.fill()
					ctx.clip();
					
		            ctx.drawImage(avatarres.tempFilePath, l * p, t * p, __w, __w);
		            ctx.save();
		            success()
		        }
		    })
		
		},
   //      saveFace(w,left,top,isClip,ctx,success){
   //          let p=this.p;
   //          let wxUserInfo = app.getUser();
			// if(!wxUserInfo.faceUrl){success()}
   //          let faceUrl = wxUserInfo.faceUrl.replace("http://", "https://");
   //          wx.downloadFile({
   //              url: faceUrl,
   //              success: function (avatarres) {
   //                  let __w = parseInt(w * p);

   //                  if(isClip){
   //                      ctx.beginPath();
   //                      ctx.arc(left * p + __w / 2, top * p + __w / 2, __w / 2, 0, 2 * Math.PI);
   //                      ctx.fill()
   //                      if (app.equipment.isOverVersion("1.6.0")) {
   //                          ctx.clip();
   //                      }
   //                  }
   //                  ctx.drawImage(avatarres.tempFilePath, left * p, top * p, __w, __w);
   //                  ctx.save();
   //                  success()
   //              }
   //          })
   //      },
        draw(ctx,_this,isSave,canvasId,success){
            let t=this;

            if(!canvasId)canvasId= _this.canvasId;
			console.log("draw1",ctx,canvasId);
			 
            ctx.draw(false, () =>{
				console.log("draw")
                uni.hideLoading()
                setTimeout(function () {
					console.log("canvasToTempFilePath")
                    uni.canvasToTempFilePath({
                        canvasId:canvasId,
                        success: function (res) {
                            console.log(res.tempFilePath)
							_this.imageUrl=res.tempFilePath;
                            
                            if(typeof success==='function'){
                                success()
                            }
                            if(isSave)  t.saveCard(_this);
                        },
						fail(res){console.log(res);
						
						}
                    });

                }, 250);
            }
			);

            
        },
        saveCard(_this){
			
            uni.showLoading();
            uni.saveImageToPhotosAlbum({
                filePath: _this.imageUrl,
                success: function (res) {
                   uni.hideLoading()
                    setTimeout(function () {
						uni.showModal({
							title:'成功保存图片',
							content:"已成功为您保存图片到手机相册",
							showCancel:false,
							confirmText: '知道了',
							
						})

                    },200)
                },
                fail: function (res) {
					 uni.hideLoading();
					 uni.showToast({
					 	title:'保存失败'
					 })
                 
                }
            })
        }
    }